
define(['bootstrapTable','bootstrapTableTreegrid','jqueryTreegrid','layer','iCheck'],function(bootstrapTable,bootstrapTreegrid,jqueryTreegrid,layer){
    var pid = 0 ;
    return{
        init:function(){
             getTreeTable();
            /*检索*/
            $("#search").click(function(){

                console.log("打印参数",$("#searchName").val());

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $('#bootstrapTree').bootstrapTable("refresh");

            })
           $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
                checkboxClass: 'icheckbox_flat-green',
                radioClass   : 'iradio_flat-green'
            })
           $(".addBtn").click(function(){
               $("#id").val(0);
               $("#name").val("");
               $("#code").val("");
               $("#value").val("");
               $("#remark").val("");
               $("#sort").val("");
               var acheck =  $("#bootstrapTree").bootstrapTable('getSelections')[0];
               if(typeof(acheck) != 'undefined') {
                 $("#pname").val(acheck.name);
                 pid = acheck.id;
               }else{
                   $("#pname").val("无父级");
               }
               submitDictionary();
            });
            $(".saveBtn").click(function(){
                var id =  $("#id").val();
                if(id == 0) {
                    $.ajax({// 添加
                        url: "/dataDictionaryController/add",
                        dataType: "json",
                        type: "post",
                        data: {
                            "name": $("#name").val(),
                            "code": $("#code").val(),
                            "value":$("#value").val(),
                            "remark": $("#remark").val(),
                            "sort": $("#sort").val(),
                            "pid": pid,
                        },
                        success: function (result) {
                            layer.msg('保存成功', {icon: 1})
                            layer.closeAll();
                            $('#bootstrapTree').bootstrapTable("refresh");
                        }
                    })
                }else{
                    $.ajax({// 修改
                        url: "/dataDictionaryController/update",
                        dataType: "json",
                        type: "post",
                        data: {
                            "name": $("#name").val(),
                            "code": $("#code").val(),
                            "value":$("#value").val(),
                            "remark": $("#remark").val(),
                            "sort": $("#sort").val(),
                            "id": $("#id").val(),
                            "pid": pid,
                        },
                        success: function (result) {
                            layer.msg('保存成功', {icon: 1})
                            layer.closeAll();
                            $('#bootstrapTree').bootstrapTable("refresh");
                        }
                    })
                }
            });
            //重置输入框
            $('.reset').click(function(){
                // $('.addForm')[0].reset();
                $("#name").val("");
                $("#code").val("");
                $("#remark").val("");
                $("#value").val("");
                $("#sort").val("");
            });
        }
    };

    //添加修改弹框
    function openLayer(title,$dom){
        layer.open({
            type:1,
            title:title,
            anim:1,
            closeBtn:1,
            area:['60%','60%'],
            content:$dom,
            end:function () {
                // location.reload();
            },
            cancel: function(index, layero){
                $dom.css({"display":"none"})
            }
        });
    }
    /**
     *  添加
     */
    function submitDictionary(){
        openLayer("添加数据字典",$("#editDictionary"));
        //添加输入框验证
    }

    function getTreeTable(){
        window.openEvent={
            'click #editor':function (e,value,row,index) {
                openLayer("修改信息",$("#editDictionary"));
                $('#id').val(row.id);
                $("#name").val(row.name);
                $("#code").val(row.code);
                $("#remark").val(row.remark);
                $("#value").val(row.value);
                $("#sort").val(row.sort);
                if(row.pname.length==0){
                    $("#pname").val("无父级");
                }else{
                    $("#pname").val(row.pname);
                }
            },
            'click #delete':function(e,value,row,index){
                layer.confirm('您确定要删除此项信息？', {title:'删除数据字典信息',icon: 2,
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url:"/dataDictionaryController/del",
                        dataType:"json",
                        type:"post",
                        data:{
                            "id":row.id
                        },
                        success:function(result){
                            if(result.code==10001){
                                layer.msg("删除成功！",{icon:1});
                                layer.closeAll();
                                $('#bootstrapTree').bootstrapTable("refresh");
                            }else{
                                layer.msg("删除失败！",{icon:1});
                            }

                        }
                    });
                });
            }
        };
        $("#bootstrapTree").bootstrapTable({
            url:'/dataDictionaryController/pageSelect',
            method:'post',
            sidePagenation:'server',
            idField:'id',
            dataType:"json",
            cache: false,       // 设置为 false 禁用 AJAX 数据缓存， 默认为true
            contentType:'application/x-www-form-urlencoded',
            //striped: true,      //表格显示条纹，默认为false
            pagination: true,   // 在表格底部显示分页组件，默认false
            pageList: [20,50,100], // 设置页面可以显示的数据条数
            pageSize: 30,       // 页面数据条数
            pageNumber: 1,      // 首页页码
            queryParamsType : "",
            queryParams:function( ) {
                return tem = {
                    name: $("#searchName").val(),
                }
            },
            columns:[
                {
                    field: 'ck',
                    checkbox: true,
                    width:'1%'
                },{
                    field:'name',
                    align: 'left',
                    valign: 'middle',
                    title:'名称',
                    width:'15%'
                },{
                    field:'code',
                    align: 'left',
                    valign: 'middle',
                    title:'编码',
                    width:'12%'
                },{
                    field:'value',
                    align: 'left',
                    valign: 'middle',
                    title:'值',
                    width:'12%'
                },{
                    field:'remark',
                    align: 'left',
                    valign: 'middle',
                    title:'备注',
                    width:'12%'
                },{
                    field:'sort',
                    align: 'left',
                    valign: 'middle',
                    title:'排序',
                    width:'12%'
                },{
                    field:'createTime',
                    align: 'left',
                    valign: 'middle',
                    title:'创建时间',
                    width:'12%'
                },{
                    field:'createUser',
                    align: 'left',
                    valign: 'middle',
                    title:'创建人',
                    width:'12%'
                } ,{
                    field:'operator',
                    align: 'left',
                    valign: 'middle',
                    title:'操作',
                    events:openEvent,
                    width:'13%',
                    formatter:function(value,row,index){
                        return ['<button class="btn btn-xs btn-info btn-operator-gew" id="editor"><i class="fa fa-edit"></i></button>','<button class="btn btn-xs btn-danger" id="delete"><i class="fa fa-trash-o"></i></button>'].join("");
                    }
                }
            ],
            treeShowField: 'name',
            parentIdField: 'pid',
            onLoadSuccess: function(data) {
                $("#bootstrapTree").treegrid({
                    initialState: 'collapsed',//收缩
                    treeColumn: 1,//指明第几列数据改为树形
                    expanderExpandedClass: 'fa fa-chevron-circle-down',
                    expanderCollapsedClass: 'fa fa-chevron-circle-right',
                    onChange: function() {

                        $("#bootstrapTree").bootstrapTable('resetWidth');
                    }
                });
                //$('#bootstrapTree').bootstrapTable("refresh");
            }
        });

    };

});